﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bluyel.Core;

namespace Bluyel_Samples_VS10
{
    /// <summary>
    /// Code samples to see how to create:
    /// Type and var/const of LOV String.
    /// </summary>
    public class Sample_LOVString
    {
        //---------------------------------------------------------------------
        /// <summary>
        /// Create a type LOV with items string.
        /// remove typeLOV.
        /// </summary>
        public void CreateTypeLOVStringRemoveOne()
        {
            Console.WriteLine("\n----Create some typeLOV, remove one:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // create typeLOV, values are typeof string
            Console.WriteLine("Create a TypeLOV string items, name=typeLOVWeekDay");
            ITypeLOVString typeLOVWeekDay = core.TypeBuilder.CreateTypeLOVString("typeLOVWeekDay");
            core.TypeBuilder.CreateTypeLOVString("typeLOV-A");

            // Display all types LOV: display the defined LOV
            IEnumerable<ITypeLOV> listType = core.TypeFinder.ListTypeLOV;
            Console.WriteLine("TypeLOV defined count: " + listType.Count() + "\n");
            foreach (ITypeLOV type in listType)
                Console.WriteLine(type.Id + " " + type.Name);

            // remove the typeLOV, display again all types
            bool res = core.TypeBuilder.RemoveTypeLOVByName("typeLOV-A");
            Console.WriteLine("\nRemove typeLOV-A, result= " + res);

            // Display again all types
            listType = core.TypeFinder.ListTypeLOV;
            Console.WriteLine("TypeLOV defined count: " + listType.Count() + "\n");
            foreach (ITypeLOV type in listType)
                Console.WriteLine(type.Id + " " + type.Name);


            ITypeLOV typeLOVWeekDayFind = core.TypeFinder.FindTypeLOVByName("typeLOVWeekDay");

            string found = "No";
            if (typeLOVWeekDayFind != null)
                found = "Yes!";
            Console.WriteLine("Find the TypeLOV by the name: typeLOVWeekDay, found=" + found);
        }

        //---------------------------------------------------------------------
        /// <summary>
        // Create a type LOV, add items, remove, find
        /// </summary>
        public void CreateTypeLOVString_AddRemoveItems()
        {
            Console.WriteLine("\n----Create a type LOV, add and remove items:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // create typeLOV, values are typeof string
            Console.WriteLine("Create a TypeLOV string items, name=typeLOVWeekDay");
            ITypeLOVString typeLOVWeekDay = core.TypeBuilder.CreateTypeLOVString("typeLOVWeekDay");

            // add value to the LOV
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Monday");
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Thursday");
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Wenesday");
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Blabla");

            // display all items of the typeLOV
            Console.WriteLine("Items of the TypeLOV typeLOVWeekDay:");
            foreach (string item in typeLOVWeekDay.ListValItems)
                Console.WriteLine(item);

            // remove a value from a LOV
            Console.WriteLine("\nRemove an Item from the TypeLOV typeLOVWeekDay: blabla");
            core.TypeBuilder.RemoveLOVValue(typeLOVWeekDay, "blabla");
            core.TypeBuilder.RemoveLOVValueByPos(typeLOVWeekDay, 3);

            // display all items of the typeLOV
            Console.WriteLine("Items of the TypeLOV typeLOVWeekDay:");
            foreach (string item in typeLOVWeekDay.ListValItems)
                Console.WriteLine(item);
        }

        //---------------------------------------------------------------------
        /// <summary>
        // Create a type LOV, add items, remove, find
        /// </summary>
        public void CreateTypeLOVString_AddItems_FindIt()
        {
            Console.WriteLine("\n----Create a type LOV, add items and find it:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // create typeLOV, values are typeof string
            Console.WriteLine("Create a TypeLOV string items, name=typeLOVWeekDay");
            ITypeLOVString typeLOVWeekDay = core.TypeBuilder.CreateTypeLOVString("typeLOVWeekDay");

            // add value to the LOV
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Monday");
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Thursday");
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Wenesday");

            // check if the typeLOV contains a value 
            if (core.TypeFinder.TypeLOVContains(typeLOVWeekDay, "Monday"))
                Console.WriteLine("Yes, the typeLOV contains the item: Monday");

            // find a value by the value
            Console.WriteLine("\nCheck Find an item by the value: Wenesday");
            DValString itemWenesday= core.TypeFinder.FindValueInTypeLOV(typeLOVWeekDay, "Wenesday");

            if (itemWenesday != null)
                Console.WriteLine("\nValue Wenesday was found in the typeLOV typeLOVWeekDay!");
            else
                Console.WriteLine("\nValue Wenesday was not found in the typeLOV typeLOVWeekDay.");
        }


        //---------------------------------------------------------------------
        /// <summary>
        // Create a type LOV, add items, remove, find
        /// </summary>
        public void CreateTypeLOVString_CreateVar()
        {
            Console.WriteLine("\n----Create a LOV var:\n");

            // create the core and define all built-in types
            BluyelCore core = new BluyelCore();
            core.TypeBuilder.DefineAllTypes();

            // create typeLOV, values are typeof string
            Console.WriteLine("Create a TypeLOV string items, name=typeLOVWeekDay");
            ITypeLOVString typeLOVWeekDay = core.TypeBuilder.CreateTypeLOVString("typeLOVWeekDay");

            // add value to the LOV
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Monday");
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Thursday");
            core.TypeBuilder.AddLOVValue(typeLOVWeekDay, "Wenesday");

            foreach (string item in typeLOVWeekDay.ListValItems)
                Console.WriteLine(item);

            // create data typeof LOV
            // the default mode is: Single(Single, Many), the default selected value is the first one (or none?).
            Console.WriteLine("\nNow create a var typeof TypeLOV/typeLOVWeekDay");
            IDLOVString lovWeekDay = core.DataBuilder.CreateVarLOVString(typeLOVWeekDay, "MyLov");

            // the LOV contains a value?
            if(core.DataBuilder.LOVContainsVal(lovWeekDay, "Monday"))
                Console.WriteLine("The var LOV contains the value 'Monday'.");

            // no selected value at the creation, is null
            if (lovWeekDay.SelectedVal == null)
                Console.WriteLine("\nNo item selected at the creation of the data LOV.");
            else
                // not displayed
                Console.WriteLine("\nA item is selected at the creation of the data LOV.");

            // select an item: by pos
            DValString dvSelect = core.DataBuilder.SelectLOVStringValAt(lovWeekDay, 1);
            Console.WriteLine("Select value at the pos 1 (Thursday): " + lovWeekDay.SelectedVal.GetValue());

            // select an item: by value
            // TODO:
            core.DataBuilder.SelectLOVStringVal(lovWeekDay, "Monday");
            Console.WriteLine("Select by value (Monday): " + lovWeekDay.SelectedVal.GetValue());
            //sdsd();

            // unselect value in the data LOV ( -1 to unselect)
            IDVal dvSelectNull = core.DataBuilder.SelectLOVValAt(lovWeekDay, -1);
            // no selected value at the creation, is null
            if (lovWeekDay.SelectedVal == null)
                Console.WriteLine("Unselect item: null");
            else
                // not displayed
                Console.WriteLine("Unselect item failed!");
        }
    }
}
